﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VideoClips.WinUI.Common
{

    public class FFMEPGHelper
    {
        public static void TrimVideo(string inputPath, string outputPath)
        {
            double duration = GetVideoDuration(inputPath);
            Process process = new Process();
            process.StartInfo.FileName = "ffmpeg";
            process.StartInfo.Arguments = "-i " + inputPath + " -ss 5 -t " + (duration - 10) + " -c copy " + outputPath;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.CreateNoWindow = true;

            process.Start();
            process.WaitForExit();
        }
        public static double GetVideoDuration(string videoPath)
        {
            double duration = 0;
            Process process = new Process();
            process.StartInfo.FileName = "ffprobe";
            process.StartInfo.Arguments = "-i " + videoPath + " -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.CreateNoWindow = true;
            //process.StartInfo.RedirectStandardError = true;
            process.Start();         
            process.WaitForExit();

            string output = process.StandardOutput.ReadToEnd();
            //string err = process.StandardError.ReadToEnd();
            if (double.TryParse(output, out duration))
            {
                return duration;
            }
            else
            {
                return 0;
            }
        }

        //视频切割
        public static string Cut(string originFile/*视频源文件*/, string startTime/*开始时间*/, string endTime/*结束时间*/)
            {
                string DstFile = originFile.Replace(".", "a.");
                string strCmd = " -ss " + startTime
                    + " -i " + originFile
                    + " -to " + endTime
                    + " -vcodec copy -acodec copy " + DstFile + " -y ";
                if (System.IO.File.Exists(DstFile)) System.IO.File.Delete(DstFile);
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = "ffmpeg.exe";//要执行的程序名称
                p.StartInfo.Arguments = " " + strCmd;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = false;//可能接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = false;//由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = false;//重定向标准错误输出
                p.StartInfo.CreateNoWindow = false;//不显示程序窗口

                p.Start();//启动程序
                p.WaitForExit();//等待程序执行完退出进程

                if (System.IO.File.Exists(DstFile))
                {
                    return DstFile;
                }
                return "";
            }           
    }
}
